package geektime.spring.springbucks;

import com.github.pagehelper.PageHelper;
import geektime.spring.springbucks.api.IpInfoService;
import geektime.spring.springbucks.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Properties;
import java.util.Set;

// import geektime.spring.springbucks.service.CoffeeOrderService;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class SpringBucksApplication implements ApplicationRunner {

	@Autowired
	IpInfoService ipInfoService;

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@Autowired
	private DiscoveryClient discoveryClient;

	private void showServiceInstances() {
		log.info("DiscoveryClient: {}", discoveryClient.getClass().getName());
		discoveryClient.getInstances("coffee-service").forEach(s -> {
			log.info("Host: {}, Port: {}", s.getHost(), s.getPort());
		});
		ipInfoService.getIpInfo();
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		showServiceInstances();

	}

	
	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}


}

